package com.sptpc.demo1.controller;

import com.sptpc.demo1.model.Category;
import com.sptpc.demo1.model.Drink;
import com.sptpc.demo1.model.Student;
import com.sptpc.demo1.service.ICategoryService;
import com.sptpc.demo1.service.IDrinkService;
import com.sptpc.demo1.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class RouterController {
    @Autowired
    public IDrinkService iDrinkService;
    @Autowired
    public IStudentService iStudentService;
    @Autowired
    public ICategoryService iCategoryService;

    //界面管理
    @GetMapping("/")
    public String Login(){
        return "manage/login";
    }
    @GetMapping("manage/index")
    public String Index(){
        return "manage/index";
    }
    @GetMapping("manage/welcome")
    public String welcome(){
        return "manage/welcome";
    }

    //饮品drink接口转界面
    @GetMapping("/menu/drink")
    public String drinkindex(){
        return "drink/index";
    }
    @GetMapping("/drink/addform")
    public String drinkaddForm(){
        return "drink/add";
    }
    @GetMapping("/drink/updateform")
    public ModelAndView updateform(@RequestParam Integer id){
        //ModelAndView类似于Intent，因为java不能同时传递数据和页面。
        ModelAndView modelAndView=new ModelAndView("drink/edit");
        //从数据库通过id查询出对应的饮品信息
        Drink drink=iDrinkService.getDrinkById(id);

        modelAndView.addObject("data",drink);//addObject()方法将数据传递类似intent的putexpar。
        return modelAndView;
    }

    //学生stu接口转界面
    @GetMapping("/menu/stu")
    public String stuindex(){
        return "stu/index";
    }
    @GetMapping("/stu/addform")
    public String stuaddForm(){
        return "stu/add";
    }
    @GetMapping("/stu/updateform")
    public ModelAndView stuupdateform(@RequestParam Long id){
        ModelAndView modelAndView=new ModelAndView("stu/edit");
        //从数据库通过id查询出对应的学生信息
        Student student=iStudentService.getStudentById(id);
        modelAndView.addObject("data",student);
        return modelAndView;
    }


    //category分类接口转界面
    @GetMapping("/menu/category")
    public String categoryindex(){
        return "category/index";
    }
    @GetMapping("/category/addform")
    public String categoryaddForm(){
        return "category/add";
    }
    @GetMapping("/category/updateform")
    public ModelAndView categoryupdateform(@RequestParam Integer id){
        ModelAndView modelAndView=new ModelAndView("category/edit");
        //从数据库通过id查询出对应的学生信息
        Category category=iCategoryService.getCategoryById(id);
        modelAndView.addObject("data",category);
        return modelAndView;
    }
}
